<?php

namespace Ticket\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Ticket\Entity\Ticket;

class TicketController extends AbstractActionController {

    protected $entityManager;
    protected $repository;

    public function indexAction() {
        return new ViewModel(array(
            'tickets' => $this->getRepository()->findAll(),
        ));
    }

    public function viewAction() {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) { // TODO: superflous if
            return $this->redirect()->toRoute('ticket', array('action' => 'add'));
        }

        $ticket = $this->getRepository()->find($id);
        $ticketUpdate = new \Ticket\Entity\TicketUpdate();

        $updateForm = $this->getTicketUpdateForm();
        $updateForm->bind($ticketUpdate);
        $updateForm->setAttribute('action', $this->url()->fromRoute('ticket', array(
                    'action' => 'view',
                    'id' => $id,
        )));

        $request = $this->getRequest();
        if ($request->isPost()) {
            $updateForm->setData($request->getPost());

            if ($updateForm->isValid()) {
                $ticketUpdate->setAuthor($this->getAuthenticationService()->getIdentity());
                $ticketUpdate->setTicket($ticket);
                $ticketUpdate->setTime(new \DateTime("now"));

                $this->getEntityManager()->persist($ticketUpdate);
                $this->getEntityManager()->flush();
                return $this->redirect()->toRoute('ticket', array(
                            'action' => 'view',
                            'id' => $id,
                ));
            }
        }

        return new ViewModel(array(
            'ticket' => $ticket,
            'updateForm' => $updateForm,
        ));
    }

    public function addAction() {
        $ticket = new Ticket();

        $form = $this->getTicketForm();
        $form->setAttribute('action', $this->url()->fromRoute('ticket', array('action' => 'add')));
        $form->get('submit')->setValue('Add');
        $form->bind($ticket);

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $ticket->setTime(new \DateTime());
                $ticket->setUser($this->getAuthenticationService()->getIdentity());
                $this->getEntityManager()->persist($ticket);
                $this->getEntityManager()->flush();
                return $this->redirect()->toRoute('ticket');
            }
        }
        return array('form' => $form);
    }

    public function editAction() {
        // TODO: ADD ROLE
        // TODO: ADMIN ROLE SHOULD NOT BE EDITED!
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('ticket', array('action' => 'add'));
        }
        $ticket = $this->getRepository()->find($id);

        $form = $this->getTicketForm();
        $form->bind($ticket);
        $form->get('submit')->setAttribute('value', 'Edit');
        $form->setAttribute('action', $this->url()->fromRoute('ticket', array(
                    'action' => 'edit',
                    'id' => $id,
        )));

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost()); // TODO: DO NOT BIND ID!

            if ($form->isValid()) {
                $this->getEntityManager()->persist($ticket);
                $this->getEntityManager()->flush();
                return $this->redirect()->toRoute('ticket');
            }
        }

        return array(
            'id' => $id,
            'form' => $form,
        );
    }

    public function deleteAction() {
        // TODO: CANT DELETE ADMIN ACCOUNT!
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('ticket');
        }

        $ticket = $this->getRepository()->find($id);

        $request = $this->getRequest();
        if ($request->isPost()) {
            $del = $request->getPost('del', 'No');

            if ($del == 'Yes') {
                $id = (int) $request->getPost('id');
                $this->getRepository()->remove($ticket);
            }

            // Redirect to list of tickets
            return $this->redirect()->toRoute('ticket');
        }

        return array(
            'ticket' => $ticket,
        );
    }

    public function closeAction() {
        assert($this->getRequest()->isPost());

        $id = (int) $this->params()->fromRoute('id', 0);
        assert($id != 0);

        $ticket = $this->getRepository()->find($id);
        $ticket->setClosed(!$ticket->getClosed());

        $this->getEntityManager()->persist($ticket);
        $this->getEntityManager()->flush();
        return $this->redirect()->toRoute('ticket', array('action' => 'view', 'id' => $ticket->getId()));
    }

    /**
     * @return \Doctrine\ORM\EntityManager
     */
    public function getEntityManager() {
        if (!$this->entityManager) {
            $this->entityManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }

        return $this->entityManager;
    }

    /**
     * @return \Ticket\Repository\TicketRepository
     */
    public function getRepository() {
        if (!$this->repository) {
            $this->repository = $this->getServiceLocator()->get('Ticket\Repository\TicketRepository');
        }

        return $this->repository;
    }

    public function getTicketForm() {
        return $this->getServiceLocator()->get('Ticket\Form\TicketForm');
    }

    public function getTicketUpdateForm() {
        return $this->getServiceLocator()->get('Ticket\Form\TicketUpdateForm');
    }

    public function getAuthenticationService() {
        return $this->getServiceLocator()->get('Authentication\Service\AuthenticationService');
    }

}